﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MyOA.Model;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class LoginLogService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 根据登陆时间来获取登陆信息
        /// </summary>
        /// <param name="startTime">登陆时间</param>
        /// /// <param name="endTime">登陆时间</param>
        /// <param name="size">一页显示多少条数据</param>
        /// <param name="currentPage">当前是第几页</param>
        /// <param name="isCount">是否要进行计算总行数的操作0是进行，因为这个操作只进行一次</param>
        /// <param name="sum">out所有登陆信息的总数</param>
        /// <returns>返回符合条件的登陆信息</returns>
        public IList<LoginLog> getLoginLogByLoginTime(DateTime startTime, DateTime endTime, int size, int currentPage, int isCount, out int sum)
        {
            sum = 0;
            IList<LoginLog> loginLogs = new List<LoginLog>();
            SqlCommand command = new SqlCommand("sp_getLoginLogByLoginTime", db.Connection);
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter[] value = new SqlParameter[]
           {
               new SqlParameter("@size",size),
               new SqlParameter("@currentPage",currentPage),
               new SqlParameter("@startTime",startTime),
               new SqlParameter("@endTime",endTime),
               new SqlParameter("@isCount",isCount),
               new SqlParameter("@sum",sum)
           };
            command.Parameters.AddRange(value);
            command.Parameters["@sum"].Direction = ParameterDirection.Output;

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    LoginLog loginLog = new LoginLog();
                    loginLog.LoginId = (int)reader[0];
                    loginLog.LoginTime = (DateTime)reader[1];
                    int sucess = (int)reader[2];
                    loginLog.IfSuccess = sucess == 1 ? "成功" : "失败";
                    loginLog.LoginUserIp = (string)reader[3];
                    loginLog.User.UserId = (int)reader[4];

                    loginLogs.Add(loginLog);
                }
            }
            sum = (int)command.Parameters["@sum"].Value;
            db.CloseConnection();

            //获取登陆名
            foreach (LoginLog item in loginLogs)
            {
                item.User.LoginId = getLoginIdByUserId(item.User.UserId);
            }
            return loginLogs;
        }

        /// <summary>
        /// 根据用户ID找登陆名
        /// </summary>
        /// <param name="userId">登陆ID</param>
        /// <returns>返回登陆名</returns>
        private string getLoginIdByUserId(int userId)
        {
            return (string)db.getScalar("sp_getLoginIdByUserId", new SqlParameter("@userId", userId));
        }

        /// <summary>
        /// 根据登陆日志ID删除登陆日志
        /// </summary>
        /// <param name="loginLogId">登陆日志ID</param>
        /// <returns>删除成功与否</returns>
        public bool deleteLoginLogByLoginLogId(int loginLogId)
        {
            if (db.getCount("sp_deleteLoginLogByLoginLogId", new SqlParameter("@loginLogId", loginLogId)) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据登陆日志ID集合删除登陆日志
        /// </summary>
        /// <param name="loginLogIds">登陆日志ID集合</param>
        /// <returns>删除成功与否</returns>
        public bool deleteLoginLogByLoginLogId(string loginLogIds)
        {
            if (db.getCount("sp_deleteLoginLogsByLoginLogId", new SqlParameter("@loginLogIds", loginLogIds)) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
